In [1]:
import plotly.offline as pyo

from plotly.graph_objs import *

import chart_studio.plotly as py

import pandas as pd
from pandas import DataFrame
In [2]:
pyo.offline.init_notebook_mode()
In [3]:
lifeExpectancy = pd.read_csv(r"../Data/LifeExpectancyCigarettePrices.csv", index_col = 0)
lifeExpectancy.head()
Out[3]:
Region Country Sex Most sold cigarette brand (US$) Years
0 Eastern Mediterranean Afghanistan Male 0.22 15
1 Eastern Mediterranean Afghanistan Female 0.22 16
2 Europe Albania Male 1.43 18
3 Europe Albania Female 1.43 20
4 Africa Algeria Male 1.14 18
In [4]:
lifeExpectancy['text'] = lifeExpectancy.apply(lambda x: 
    "<b>{}</b><br>Life expectancy for {}s at 60: {} years<br>Price of cigarettes: ${:.2f}".format(x['Country'], 
                                                                                  x['Sex'],
                                                                                x['Years'],
                                                                                 float(x['Most sold cigarette brand (US$)'])), axis = 1)
In [5]:
lifeExpectancy.loc[0, 'text']
Out[5]:
'<b>Afghanistan</b><br>Life expectancy for Males at 60: 15 years<br>Price of cigarettes: $0.22'
In [6]:
regions = list(lifeExpectancy['Region'].unique())
regions
Out[6]:
['Eastern Mediterranean',
 'Europe',
 'Africa',
 'Americas',
 'Western Pacific',
 'South-East Asia']
In [7]:
sexes = list(lifeExpectancy['Sex'].unique())
sexes
Out[7]:
['Male', 'Female']
In [8]:
markerLookup = {'Eastern Mediterranean' : {'symbol' : 'circle'},
                     'Europe' :           {'symbol' : 'square'},
                     'Africa' :           {'symbol' : 'diamond'},
                     'Americas' :         {'symbol' : 'triangle-up'},
                     'Western Pacific' :  {'symbol' : 'cross'},
                     'South-East Asia' :  {'symbol' : 'x'},
                'Male' : {'color' : '#663399'}, 
                'Female' :{'color' : '#FF6347'}}
In [9]:
traces = []

for sex in sexes:
    for reg in regions:
        traces.append({'type' : 'scatter',
                      'mode' : 'markers',
                      'x' : lifeExpectancy.loc[(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex),
                                               'Most sold cigarette brand (US$)'],
                        'y' : lifeExpectancy.loc[(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex), 'Years'],
                       'text' : lifeExpectancy.loc [(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex),'text'],
                       'hoverinfo' : 'text',
                       'marker' : {'color' : markerLookup[sex]['color'],
                                   'symbol' : markerLookup[reg]['symbol'],
                                  'opacity' : 0.7},
                      'name' : "{}, {}".format(reg, sex)})
In [10]:
layout = {'title' : 'Life Expectancy Against Price of Most Popular Brand of Cigarettes (2011)',
         'xaxis' : {'title' : 'Price of most popular brand of cigarettes',
                    'range' : [0, 
                               lifeExpectancy['Most sold cigarette brand (US$)'].max() * 1.05],
                   'tickformat' : "${:}"},
         'yaxis' : {'title' : 'Life expectancy at age 60 (years)',
                    'range' : [lifeExpectancy['Years'].min()*0.9, 
                              lifeExpectancy['Years'].max()*1.05],},
         'hovermode' : 'closest'}
In [12]:
fig = Figure(data=traces, layout=layout)
pyo.iplot(fig)
In [13]:
traces = []

for sex in sexes:
    for reg in regions:
        traces.append({'type' : 'scatter',
                      'mode' : 'markers',
                      'x' : lifeExpectancy.loc[(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex),
                                               'Most sold cigarette brand (US$)'],
                        'y' : lifeExpectancy.loc[(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex), 'Years'],
                       'text' : lifeExpectancy.loc [(lifeExpectancy['Region'] == reg) & (lifeExpectancy['Sex'] == sex),'text'],
                       
                       'legendgroup' : reg,                 
                       
                       'hoverinfo' : 'text',
                       'marker' : {'color' : markerLookup[sex]['color'],
                                   'symbol' : markerLookup[reg]['symbol'],
                                  'opacity' : 0.7},
                      'name' : "{}, {}".format(reg, sex)})
In [14]:
fig = Figure(data=traces, layout=layout)
pyo.iplot(fig)
In [ ]: